// storage/mysql_storage.hpp
#pragma once

#include <vector>
#include <string>
#include <map>
#include <memory>
#include <any>
#include <mysql/mysql.h>
#include "mysql_pool.hpp"

// 查询结果行
class ResultRow {
public:
    template<typename T>
    T get(const std::string& column_name) const {
        auto it = data_.find(column_name);
        if (it == data_.end() || it->second.empty()) {
            throw std::runtime_error("Column not found or is null: " + column_name);
        }
        
        if constexpr (std::is_same_v<T, std::string>) {
            return it->second;
        } else if constexpr (std::is_same_v<T, int>) {
            return std::stoi(it->second);
        } else if constexpr (std::is_same_v<T, int64_t>) {
            return std::stoll(it->second);
        } else if constexpr (std::is_same_v<T, double>) {
            return std::stod(it->second);
        } else if constexpr (std::is_same_v<T, bool>) {
            return it->second == "1" || it->second == "true";
        } else {
            static_assert(std::is_same_v<T, void>, "Unsupported type");
        }
    }
    
    bool is_null(const std::string& column_name) const {
        auto it = nulls_.find(column_name);
        return it != nulls_.end() && it->second;
    }
    
    void set_value(const std::string& column_name, const std::string& value) {
        data_[column_name] = value;
        nulls_[column_name] = false;
    }
    
    void set_null(const std::string& column_name) {
        data_[column_name] = "";
        nulls_[column_name] = true;
    }
    
private:
    std::map<std::string, std::string> data_;
    std::map<std::string, bool> nulls_;
};

// 预处理语句包装
class PreparedStatement {
public:
    void bind_param(int index, const std::string& value);
    void bind_param(int index, int64_t value);
    void bind_param(int index, double value);
    // ... 其他类型的绑定
private:
    MYSQL_STMT* stmt_;
    std::vector<MYSQL_BIND> binds_;
};

class MysqlManager {
public:
    explicit MysqlManager(const MySQLConfig& config);
    ~MysqlManager() = default;
    // // 事务支持
    // class Transaction {
    // public:
    //     explicit Transaction(std::shared_ptr<MysqlConnection> conn);
    //     void commit();
    //     void rollback();
    //     ~Transaction();
    // private:
    //     std::shared_ptr<MysqlConnection> conn_;
    //     bool committed_ = false;
    // };

    // 高级查询接口
    std::vector<ResultRow> query(const std::string& sql, 
                                const std::vector<std::any>& params = {});
    
    int64_t execute(const std::string& sql, 
                    const std::vector<std::any>& params = {});
    
    // 事务支持
    // std::unique_ptr<Transaction> begin_transaction();

    // 预处理语句
    // std::shared_ptr<PreparedStatement> prepare(const std::string& sql);

protected:
    // 辅助方法
    void bind_params(MYSQL_STMT* stmt, const std::vector<std::any>& params);
    std::vector<ResultRow> fetch_results(MYSQL_STMT* stmt);

    std::unique_ptr<MySQLPool> mysql_pool_;
};